#include <stdio.h>			//include file io library
#include <stdarg.h>
#include <string.h>
//#include <afx.h>

#include "Log.h"

namespace helix
{

	Log::Log()
	{
		fileName=new char[strlen("logfile.txt")+1];
		strcpy(fileName,"logfile.txt");                 //get the name of the file
		fileName[strlen("logfile.txt")]='\0';
			
		if ((logFile=fopen(fileName,"rt"))!=NULL)	//if the log file alread exists
		{
			fclose(logFile);					//close the file
			remove(fileName);					//and delete it
		}

		const char *fName=fileName;
		logFile=fopen(fName,"at");		//open the file
		fprintf(logFile,"Log File Created\n");	//output to the file
		fclose(logFile);						//close the file
	}

	Log::Log(char *logFileName)
	{
		fileName=new char[strlen(logFileName)+1];
		strcpy(fileName,logFileName);
		fileName[strlen(logFileName)]='\0';

		if ((logFile=fopen(fileName,"rt"))!=NULL)	//if the log file alread exists
		{
			fclose(logFile);					//close the file
			remove(fileName);					//and delete it
		}

		const char *fName=fileName;
		logFile=fopen(fName,"at");		//open the file
		fprintf(logFile,"Log File Created\n");	//output to the file
		fclose(logFile);						//close the file
	}

	void Log::releaseLog()
	{
		if(fileName)
			delete[] fileName;
	}

	void Log::setLog(char *logFileName)
	{
		delete [] fileName;
		fileName=new char[strlen(logFileName)+1];
		strcpy(fileName,logFileName);
		fileName[strlen(logFileName)]='\0';

		if ((logFile=fopen(fileName,"rt"))!=NULL)	//if the log file alread exists
		{
			fclose(logFile);					//close the file
			remove(fileName);					//and delete it
		}

		const char *fName=fileName;
		logFile=fopen(fName,"at");		//open the file
		fprintf(logFile,"Log File Created\n");	//output to the file
		fclose(logFile);						//close the file
	}

	void Log::Print(char *outString,...)
	{
		char		text[256];			// Holds Our String
		va_list		ap;					// Pointer To List Of Arguments
		if (outString == NULL)			// If There's No Text
			return;						// Do Nothing
		va_start(ap, outString);		// Parses The String For Variables
		(void)_vsnprintf(text,sizeof(text), outString, ap);	// And Converts Symbols To Actual Numbers
		va_end(ap);						// Results Are Stored In Text

		const char *fName=fileName;
		logFile=fopen(fName,"at");		//open the file
		fprintf(logFile,"%s",text);		//print the text
		fclose(logFile);				//close the file
	}

	void Log::Println(char *outString,...)
	{
		char		text[256];				// Holds Our String
		va_list		ap;					// Pointer To List Of Arguments
		if (outString == NULL)			// If There's No Text
			return;						// Do Nothing
		va_start(ap, outString);		// Parses The String For Variables
		(void)_vsnprintf(text,sizeof(text), outString, ap);	// And Converts Symbols To Actual Numbers
		va_end(ap);						// Results Are Stored In Text

		const char *fName=fileName;
		logFile=fopen(fName,"at");		//open the file
		fprintf(logFile,"%s\n",text);	//print the text and carriage return
		fclose(logFile);				//close the file
	}


	void Log::Error(char *outString,...)
	{
		char		text[256];			// Holds Our String
		va_list		ap;					// Pointer To List Of Arguments
		if (outString == NULL)			// If There's No Text
			return;						// Do Nothing
		va_start(ap, outString);		// Parses The String For Variables
		(void)_vsnprintf(text,sizeof(text), outString, ap);	// And Converts Symbols To Actual Numbers
		va_end(ap);						// Results Are Stored In Text

		const char *fName=fileName;
		logFile=fopen(fName,"at");		//open the file
		fprintf(logFile,"****ERROR****{   %s   }****ERROR****\n",text);	//print error message
		fclose(logFile);				//close the file
	}

	FILE* Log::getFilePtr()
	{
		return logFile;
	}

	void Log::deleteLog()
	{
		if ((logFile=fopen(fileName,"rt"))!=NULL)	//if the log file alread exists
		{
			fclose(logFile);					//close the file
			if(-1==remove(fileName))					//and delete it
			{
				Error("Error deleting file:%s",fileName);
				return;
			}
		}
		logFile=NULL;
	}

}